
//文件的操作
const {join}=require('path');
const Table=require('cli-table');//绘图
const {
    existsSync,
    writeFileSync,
    readdirSync,
    unlinkSync,
    renameSync,
    statSync
}=require('fs');

class Utils{
    constructor (){
        console.log(__dirname,555);
        console.log(process.cwd(),666);
        this.folderPath=join(process.cwd(),'src');
    }
    //公有方法，实例调用
    addFile(filename){//添加文件
        //检测文件是否存在
        let newpath=join(this.folderPath,filename);
        // console.log(newpath,this.folderPath);
        if(existsSync(newpath)){
            console.log('文件已存在');
        }else{
            writeFileSync(newpath,'添加成功');//写入文件
            console.log('文件创建成功');
        }
    }

    getFile(){//获取文件目录
        return readdirSync(this.folderPath)
    }

    delFlie(filename){//删除文件
        let newpath=join(this.folderPath,filename);
        unlinkSync(newpath);
        console.log('删除文件成功');
    }

    editFile({filename,newfile}){
        let oldpath=join(this.folderPath,filename);
        let newpath=join(this.folderPath,newfile);
        if(existsSync(newpath)){
            console.log('文件名重复');
        }else{
            renameSync(oldpath,newpath);
            console.log('文件名重命名成功');

        }
    }

    checkFile(filename){
        let newpath=join(this.folderPath,filename);
        // console.log(statSync(newpath));
        const {
            size,
            ctime,
            mtime
        }=statSync(newpath)
        this.createTable(size,ctime,mtime,filename)
    }
    createTable(size,ctime,mtime,filename){
        const table=new Table({
            head:['文件名','更改时间','创建时间','文件大小'],
            colWidths:[20,40,40,30],
    });
    table.push([filename,
        ctime.toLocaleString(),
        mtime.toLocaleString(),
        size+'b']);

        console.log(table.toString());
    }
};

// exports.utils=new Utils()
// let utils=new Utils()
// module.exports={
//     utils
// }
module.exports=new Utils();//无需解构